package com.hd.trans.utils;

import android.hardware.Camera;

import java.util.List;

/**
 * Created by gxj on 2018/2/8.
 */
public class CamParaUtil {

    /**
     * 打开相机
     */
    public static Camera openCamera() {
        Camera c = null;
        try {
            c = Camera.open();
        } catch (Exception e) {
        }
        return c;
    }

    /**
     * Android相机的预览尺寸都是4:3或者16:9，这里遍历所有支持的预览尺寸，得到16:9的最大尺寸，保证成像清晰度
     *
     * @param sizes
     * @return 最佳尺寸
     */
    public static Camera.Size getBestSize(List<Camera.Size> sizes) {

        Camera.Size bestSize = null;
        for (Camera.Size size : sizes) {
            if ((float) size.width / (float) size.height == 16.0f / 9.0f) {
                if (bestSize == null) {
                    bestSize = size;
                } else {
                    if (size.width > bestSize.width) {
                        bestSize = size;
                    }
                }
            }
        }
        return bestSize;
    }

    /**
     * 理想的显示宽高w,h
     */
    public static Camera.Size getBestSize(List<Camera.Size> sizes, int w, int h, boolean isHd) {
        //final double ASPECT_TOLERANCE = 0.1;
        float mW = (float) Math.max(w, h);
        float mH = (float) Math.min(w, h);
        float targetRatio = mW/mH;
        double minDiff = Double.MAX_VALUE;
        int targetHeight = Math.min(w, h);
        Camera.Size bestSize = null;

        for (Camera.Size size : sizes) {
            if(isHd && size.height < 720)continue;
            double ratio = size.width / size.height;

            //if(Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
            if(Math.abs(ratio - targetRatio) <= minDiff){
                minDiff  = ratio - targetRatio;

                if (bestSize == null) {
                    bestSize = size;
                } else {
                    if (size.width > bestSize.width) {//取最高分辨率
                        bestSize = size;
                    }
                }
            }
        }

        if(bestSize == null){
            minDiff = Double.MAX_VALUE;
            for (Camera.Size size : sizes) {
                if(isHd && size.height < 720)continue;
                if(Math.abs(size.height - targetHeight) < minDiff){
                    bestSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return bestSize;
    }

    public static void turnOn(Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
    }

    public static void turnoff(Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
    }

    public static void turnAuto(Camera camera) {
        Camera.Parameters parameters = camera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
        camera.setParameters(parameters);
    }
}
